<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="商品名称" prop="brandName">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入商品名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品分类" prop="productCategoryName">
        <el-cascader size="small" clearable :value="productCategoryIdValue"
                     @input="val=>{
                       queryParams.productCategoryId = val[val.length-1];
                       productCategoryIdValue = val
                     }"
                     :options="productCategoryOptions" :props="{label:'name', value:'id'}" />
      </el-form-item>
      <el-form-item label="商品品牌">
        <el-select size="small" v-model="queryParams.brandId" clearable>
          <el-option size="small" v-for="item in brandList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="上架状态">
        <el-select size="small" v-model="queryParams.publishStatus" clearable>
          <el-option v-for="option in publishStatusOptions" :key="option.value" :label="option.label" :value="option.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select size="small" v-model="queryParams.verifyStatus" clearable>
          <el-option v-for="option in verifyStatusOptions" :key="option.value" :label="option.label" :value="option.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <div style="display: flex">
          <el-input
              v-model="queryParams.params.beginPrice"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
          <span class="ml5 mr5">—</span>
          <el-input
              v-model="queryParams.params.endPrice"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['pms:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['pms:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="商品图片" align="center">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}<br />
          <span style="margin-top: 10px;display: inline-block"> 品牌：{{ scope.row.brandName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center">
        <template slot-scope="scope">
          {{ scope.row.verifyStatus===0 ? '未审核' : '已通过' }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="销量" align="center">
        <template slot-scope="scope">
          {{ scope.row.sale ? scope.row.sale : 0 }}
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center">
        <template slot-scope="scope">
          {{ '￥' + scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center">
        <template slot-scope="scope">
          <div class="mb10">
            <span>上架：</span>
            <el-switch v-model="scope.row.publishStatus" @input="handleInput($event, scope.row, 'publishStatus')"
                       :inactive-value="0" :active-value="1"
            ></el-switch>
          </div>
          <div class="mb10">
            <span>新品：</span>
            <el-switch v-model="scope.row.newStatus" @input="handleInput($event, scope.row, 'newStatus')"
                       :inactive-value="0" :active-value="1"
            ></el-switch>
          </div>
          <div class="mb10">
            <span>推荐：</span>
            <el-switch v-model="scope.row.recommendStatus" @input="handleInput($event, scope.row, 'recommendStatus')"
                       :inactive-value="0" :active-value="1"
            ></el-switch>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['pms:product:edit']"
          >编辑</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['pms:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script>
import { listProduct, delProduct, updateProductStatus, exportProduct } from "@/api/pms/product";
import { listProductCategory } from "@/api/pms/productCategory";
import { listBrand } from "@/api/pms/brand";

export default {
  name: "Product",
  components: {
  },
  watch: {
    $route: {
      handler(to) {
        if (to.name !== 'Product') {
          return;
        }
        this.getList();
      },
      deep: true,
      immediate: true
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      productList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        price: null,
        productCategoryId: null,
        brandId: null,
        publishStatus: null,
        verifyStatus: null,
        params: {
          beginPrice: undefined,
          endPrice: undefined
        }
      },
      productCategoryOptions: [],
      productCategoryIdValue: [],
      brandList: [],
      publishStatusOptions: [
        { label: '上架', value: 1 },
        { label: '下架', value: 0 }
      ],
      verifyStatusOptions: [
        { label: '审核通过', value: 1 },
        { label: '未审核', value: 0 }
      ]
    };
  },
  created() {
    this.getList();
    this.getBrandList();
    this.getProductCategoryList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getBrandList() {
      listBrand().then(response => {
        this.brandList = response.rows;
      });
    },
    getProductCategoryList() {
      listProductCategory().then(response => {
        this.productCategoryOptions = this.handleTree(response.rows);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商品编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportProduct(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    handleUpdate(row) {
      this.$router.push({
        path: '/pms/productEdit',
        query: { id: row.id }
      });
    },
    handleInput(val, row, col) {
      updateProductStatus({ id: row.id, [col]: val }).then(response => {
        this.msgSuccess('修改成功');
      });
    }
  }
};
</script>